#include <iostream>
#include <string>
#include <list>
using namespace std;

// constructor
void Test_string1(){
  string s1;
  string s2("hello world!!!");
  string s3 = "bit";
  string s4(s2);
  string s5(s2, 6, 5);//第三个参数缺省取到字符串末尾，大于字符串长度也取到末尾。
  string s6("I love China", 5);
  string s7(10,'x');
  string s8(s2.begin()+6, s2.end()-3);

  cin >> s1;
  cout << s1 << endl;
  cout << s2 << endl;
  cout << s3 << endl;
  cout << s4 << endl;
  cout << s5 << endl;
  cout << s6 << endl;
  cout << s7 << endl;
  cout << s8 << endl;
}

//operator=
void Test_string2(){
  string s1;
  string s2 = "hello bit";//调用构造
  s1 = s2;//调用赋值重载
  cout << s1 << endl;
  s1 = "i love you";
  cout << s1 << endl;
  s1 = 'C';
  cout << s1 << endl;
}

//operator[]
void Test_string3(){
  string s1 = "hello";
  //cout << s1[0] << endl;
  cout << s1.at(0) << endl;
  s1[0] = 'H';
  cout << s1 << endl;

  int i = 0;
  for(i = 0; i<s1.size(); i++){
    s1[i]^=32;
  }
  cout << s1 << endl;

  const string s2 = "operatro[]";
  for(i = 0; i<s2.size(); i++){
    //s2[i]++; //error
    cout << s2[i] << " ";
  }
  cout << endl;
}

//iterator
void Test_string4(){
  //string类
  cout << "string:" << endl;
  string s1("hello");
  string::iterator it = s1.begin();
  while(it != s1.end()) //此处应该使用!=
  {
    (*it)^=32;
    ++it;
  }
  cout << s1 << endl;

  for(auto e : s1)
  {
    cout << e << " ";
  }
  cout << endl;

  for(auto &e : s1)
  {
    e^=32;
  }
  cout << s1 << endl;
  cout << endl;

  //list类 
  cout << "list:" <<endl;
  list<int> lt(10,2);
  list<int>::iterator lit = lt.begin();
  while(lit != lt.end())
  {
    cout << *lit << " ";
    ++lit;
  }
  cout <<endl; 
  
  for(auto e : lt)
  {
    cout << e << " ";
  }
  cout << endl;
}

void PrintString(const string &str){
  cout << "PrintString:" << endl;
  string::const_iterator cit = str.begin();
  while(cit!=str.end())
  {
    cout << *cit << " ";
    ++cit;
  }
  cout << endl;

  string::const_reverse_iterator crit = str.rbegin();
  while(crit!=str.rend())
  {
    cout << *crit << " ";
    ++crit;
  }
  cout << endl;
}


//reverse_iterator & const_iterator
void Test_string5(){
 string s1 = "hello";
 string::reverse_iterator rit = s1.rbegin();
 while(rit!=s1.rend())
 {
   cout << *rit << " ";
   ++rit;
 }
 cout << endl << endl;
 
 PrintString("I love China!!");
}

//push_back & append & operator +=
void Test_string6(){
  string s1("hello");
  s1.push_back(' ');
  s1.append("world");
  s1.append(3,'!');
  cout << s1 << endl;

  string s2 = "hello China";
  s1+=' ';
  s1+=s2;
  s1+="!!!";
  cout << s1 << endl;

  s1.append(s2.begin()+5, s2.end());
  cout << s1 << endl;
}

// reserve & resize
void Test_string7(){
  cout << "Test_string7:" <<endl;
  string s1;
  //s1.reserve(1000);
  s1.resize(10, 'a');
  size_t cap = s1.capacity();
  cout << s1 << endl;
  cout << "_capacity: " << cap << endl;
  cout << "_size: " << s1.size() << endl;
  for(int i = 0; i < 1000; i++)
  {
    s1.push_back('x');
    if(s1.capacity() != cap)
    {
      cap = s1.capacity();
      cout << "new capacity: " << cap << endl;
    }
  }
  cout << endl;
}

// other function
void Test_string8(){
  cout << "Test_string8:" <<endl;
  string s1 = "hello world!!";
  cout << s1 << endl;
  cout << s1.size() << endl;
  cout << s1.capacity() <<endl;
  cout << "after:" <<endl;
  //s1.clear();
  //cout << s1 << endl;
  //cout << s1.size() << endl;
  //cout << s1.capacity() <<endl;
  s1.reserve(50);
  //s1.resize(20,'c');
  cout << s1 << endl;
  cout << s1.size() << endl;
  cout << s1.capacity() <<endl;
}

int main(){
  //Test_string1(); 
  //Test_string2(); 
  //Test_string3(); 
  //Test_string4(); 
  //Test_string5(); 
  Test_string6(); 
  //Test_string7(); 
  //Test_string8(); 
  
  return 0;
}
